#include "main.h"
bool gb_stubecheck;   //左侧加热管检测
bool gb_stcheckdly;   //左侧加热管延迟检测
u8 gu8_SOnDlayCnt;	    //左侧加热管延迟判断
bool gb_btubecheck;   //右侧加热管检测
bool gb_btchekdly;     //右侧加热管言辞检测
u8 gu8_BOnDlayCnt;      //右侧加热管延迟判断
u8     gu8_STubeError;
u8     gs_tofaceSTubeError; //bit0: 上加热管  bit1:蒸盘外加热管 bit2:蒸盘内加热管 
u8     gu8_BTubeError;
u8     gs_tofaceBTubeError; //bit0: 循环风机  bit1:上加热管 bit2:散热风机 
u16     gu16_tubecurt;  //加热管电流
u16     gu16_tube1[6] = {0};
u16     gu16_baketube[2] = {0};

/***************************************************************************************
* Function Name  : 
* Description    :  开机检测
* Input          : 
* Return         : 
* Not            : 
***************************************************************************************/
void SWorkOnCheck(void)
{
    static bool gb_ferror = 0;
    //static bool gb_decheck = 0;//等待检测标志
    static u8 gu8_onsetp = 0;
    static u8 gu8_ondelay1 = 0;   //加热管打开延时30ms检测
    static u8 gu8_cnt1 = 0;
    static  u16  gu16_current1[6] = {0,0,0,0,0,0};
	u8 i;
//	gb_stubecheck = 0;
//	return;
	if(gb_stubecheck)
	{
        if(gb_stcheckdly)
        {
            ht_Ibom_out.perc = 0;
            ht_Obom_out.perc = 0;
            ht_Otop_out.perc = 0;
            ht_Itop_out.perc = 0;
            if(gb_btubecheck == 0)
                gb_stcheckdly = 0;
            return;
        }
        if(gu8_SOnDlayCnt)        //检测到错误需要延时一段时间检测
        {
            if(system_sec_f)
                gu8_SOnDlayCnt--;
            return;
        }
        if(gu8_ondelay1 > 0)         //
		{
            gu8_ondelay1--;
			return;
		}
		gu16_tubecurt = get_average_ad(TUBE_CURRENT_CH);
		switch(gu8_onsetp)	
		{
			case 0:
			{
				if(ht_Otop_out.perc == 0)
				{                
                    gu8_SOnDlayCnt = 0;//1;
					gu8_ondelay1 = 50;//TUBEONDELAY;                    
					gu8_cnt1 = 0; 
                    clear_bake();
                    ht_Itop_out.perc = 0;	
                    ht_Ibom_out.perc = 0;
                    ht_Obom_out.perc = 0;
                    ht_Otop_out.perc = 100;                    			
				} 
				else
				{					
					if(gu8_cnt1 < 4)
					{
						gu16_current1[gu8_cnt1] = gu16_tubecurt;
						gu8_cnt1++;
					}
					else
					{
						 bubble_sort(gu16_current1,4);
						 gu16_tube1[0] = ((gu16_current1[1] + gu16_current1[2]) >>1);
						 if(gu16_tube1[0] < 50)
						 {
                            gu8_STubeError = 0x01;
						 }						
					    gu8_onsetp++;						
                        gu8_cnt1 = 0;
                        gu8_ondelay1 = 0;  
                        ht_Otop_out.perc = 0;              
					}										
				}
			}
			break;
            case 1:
            {
                if(ht_Itop_out.perc == 0)
                {
                    ht_Itop_out.perc = 100;
                    gu8_ondelay1 = TUBEONDELAY; 
                    gu8_cnt1 = 0;                  
                }
                else
                {
                    if(gu8_cnt1 < 4)
                    {
                        gu16_current1[gu8_cnt1] = gu16_tubecurt;
                        gu8_cnt1++;
                    }
                    else
                    {
                         bubble_sort(gu16_current1,4);
                         gu16_tube1[1] = ((gu16_current1[1] + gu16_current1[2]) >>1);
                         if((gu16_tube1[1] < 50)&&(tempr_bom.cur < 90))
                         {													
                               gu8_STubeError |=  0x02;                                                                    								
                         }
                         gu8_onsetp++;
                         gu8_cnt1 = 0;
                         gu8_ondelay1 = 0;                     
                         ht_Itop_out.perc = 0;                     
                    }                     
                }
            }
            break;
			case 2:
            {
                if(ht_Ibom_out.perc == 0)
                {
                    
                    ht_Ibom_out.perc = 100;
                    gu8_ondelay1 = TUBEONDELAY;
                    gu8_cnt1 = 0;                    
                }
                else
                {
                    if(gu8_cnt1 < 4)
                    {
                        gu16_current1[gu8_cnt1] = gu16_tubecurt;
                        gu8_cnt1++;
                    }
                    else
                    {
                        bubble_sort(gu16_current1,4);
                        gu16_tube1[2] = ((gu16_current1[1] + gu16_current1[2]) >>1);
                        if(((gu16_tube1[2] < 50))&&(tempr_bom.cur < 90))
                        {         
                            //																	
                             gu8_STubeError |=  0x04;									                                                                   
                        }
    
                        if(gu8_STubeError)
                        {
                            if(gb_ferror == 0)
                            {
                                gb_ferror = 1;
                                gb_stubecheck = 1;
                                gu8_SOnDlayCnt = 1;
                           
                                gu8_STubeError = 0;
                            }
                            else
                            {
                                gb_stubecheck = 0;
                            
                            }
                        }
                        else
                        {
                            gb_stubecheck = 0;                    
                        }
                        gs_tofaceSTubeError = gu8_STubeError;
                        gu8_onsetp = 0;                            

                        gu8_cnt1 = 0;
                        gu8_ondelay1 = 0;                  
                        ht_Ibom_out.perc = 0;                         
                    }                      
                }
            }
			break;
			default :
				gb_stubecheck = 0;
			break;
		}		
	}
	else
	{
        gb_ferror = 0;
        gu8_SOnDlayCnt = 0;
        gu8_ondelay1 = 0;
        gu8_onsetp = 0;
        gu8_cnt1 = 0;
        for (i = 0; i < 6; i++)
        {
           gu16_current1[i] = 0;
        } 	
        if((steamCtrl.state != STA_WORK)&&(steamCtrl.state != STA_PAUSE))
        {

            gu8_STubeError = 0;
            gs_tofaceSTubeError = 0;
        }
	}
}
void BWorkOnCheck(void)
{

    static bool gb_decheck = 0;//等待检测标志
    static u8 gu8_onsetp = 0;
    static u8 gu8_ondelay1 = 0;   //加热管打开延时30ms检测
    static u8 gu8_cnt1 = 0;
    static  u16  gu16_current1[4] = {0,0,0,0};
	//u8 i;
//	gb_btubecheck = 0;
//	return;
	if(gb_btubecheck)
	{
        if(gb_btchekdly)
        {
            cycle_f = 0; 
            cold_f = 0;
            ht_Lbom_out.perc = 0;
            if(gb_stubecheck == 0)
                gb_btchekdly = 0;
            return;
        }
        if(gu8_BOnDlayCnt)        //检测到错误需要延时一段时间检测
        {
            if(system_sec_f)
                gu8_BOnDlayCnt--;
            return;
        }
        if(gu8_ondelay1 > 0)         //
		{
            gu8_ondelay1--;
			return;
		}
		gu16_tubecurt = get_average_ad(TUBE_CURRENT_CH);
		switch(gu8_onsetp)	
		{
			case 0:
            {
                gu8_steamDlyCnt = 3;
                if(ht_Lbom_out.perc == 0)
                {
                    // if(gu16_tubecurt >= 100)              
                    //     gu8_BOnDlayCnt = 1;  
                    gu8_BOnDlayCnt = 0;
                    clear_steam();                
                    ht_Lbom_out.perc = 100;
                    gu8_ondelay1 = 50;//TUBEONDELAY;
                    gu8_cnt1 = 0;                    
                }
                else
                {
                    if(gu8_cnt1 < 4)
                    {
                        gu16_current1[gu8_cnt1] = gu16_tubecurt;
                        gu8_cnt1++;
                    }
                    else
                    {
                        bubble_sort(gu16_current1,4);
                        gu16_baketube[0] = ((gu16_current1[1] + gu16_current1[2]) >>1);
                        if((gu16_baketube[0] < gu16_tube1[0])||(gu16_baketube[0] < 50))
                        {
                            if(gb_decheck)//已经延时2秒判断
                            {
                                           																		
                                gu8_BTubeError |=  0x04;									
                                
                            }
                            else
                            {
                                gu8_BOnDlayCnt = 1;//DELAYTIME;//DELAYTIME;       //延时2秒，加热管不关闭
                                gb_decheck = 1;
                                gu8_cnt1 = 0;
                                return;
                            }   
                        }
                            
                        gb_btubecheck = 0;
                        gs_tofaceBTubeError = gu8_BTubeError;
                        gu8_onsetp = 0;                            
                        gu8_cnt1 = 0;
                        gu8_ondelay1 = 0;                  
                        ht_Lbom_out.perc = 0; 
                        gb_decheck = 0;                       
                    }                      
                }
            }
			break;
			default :
				gb_btubecheck = 0;
			break;
		}		
	}
	else
	{
        gb_decheck = 0;
        gu8_BOnDlayCnt = 0;
        gu8_ondelay1 = 0;
        gu8_onsetp = 0;
        gu8_cnt1 = 0;
        if((bakeCtrl.state != STA_WORK)&&(bakeCtrl.state != STA_PAUSE))
        {

            gu8_BTubeError = 0;
            gs_tofaceBTubeError = 0;
        }
	}
}
